//
//  NMPOLocManager.swift
//  NMPO
//
//  Created by PowerfullClean on 2023/10/31.
//

import UIKit
import CoreLocation

let nmpo_locNoti = Notification.Name("nmpo_locNoti")

extension NMPOManager {
    func nmpo_isCanLocation(nmpo_complete:((Bool)->())? = nil) {
        if #available(iOS 14.0, *) {
            DispatchQueue.global().async {
                if (self.nmpo_loctionManager.authorizationStatus == .notDetermined || self.nmpo_loctionManager.authorizationStatus == .authorizedWhenInUse) && CLLocationManager.locationServicesEnabled() {
                    DispatchQueue.main.async {
                        nmpo_complete?(true)
                    }
                    
                    return
                }
                if self.nmpo_loctionManager.authorizationStatus == .denied {
                    DispatchQueue.main.async {
                        nmpo_complete?(false)
                    }
                    
                    return
                }
                DispatchQueue.main.async {
                    nmpo_complete?(false)
                }
                
            }
        }else {
            let nmpo_state = CLLocationManager.authorizationStatus()
            DispatchQueue.global().async {
                if (nmpo_state == .notDetermined || nmpo_state == .authorizedWhenInUse) && CLLocationManager.locationServicesEnabled() {
                    DispatchQueue.main.async {
                        nmpo_complete?(true)
                    }
                    
                    return
                }
                if nmpo_state == .denied {
                    DispatchQueue.main.async {
                        nmpo_complete?(false)
                    }
                    
                    return
                }
                DispatchQueue.main.async {
                    nmpo_complete?(false)
                }
                
            }
        }
        
    }
    func nmpo_requesAuthLocation() {
        nmpo_loctionManager.requestWhenInUseAuthorization()
    }
    func nmpo_requesLocation() {
        self.nmpo_isCanLocation(nmpo_complete: { nmpo_state in
            if !nmpo_state {
                return
            }
            self.nmpo_loctionManager.requestLocation()
        })
        
    }
    //MARK: - nmpo 请求定位权限
    func nmpo_configLocManager() {
        nmpo_loctionManager.distanceFilter = 3000
        nmpo_loctionManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
        nmpo_loctionManager.delegate = self
        nmpo_loctionManager.allowsBackgroundLocationUpdates = false
        nmpo_loctionManager.requestWhenInUseAuthorization()
    }
}
//MARK: - nmpo CLLocationManagerDelegate
extension NMPOManager: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error)
    }
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        if #available(iOS 14.0, *) {
            if manager.authorizationStatus == .authorizedWhenInUse {
                nmpo_requesLocation()
                NotificationCenter.default.post(name: nmpo_locNoti, object: nil)
            }
        } else {
            if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
                nmpo_requesLocation()
                NotificationCenter.default.post(name: nmpo_locNoti, object: nil)
            }// Fallback on earlier versions
        }
    }
    
    func nmpo_requestLocationAlertAuth() {
        nmpo_isCanLocation { nmpo_state in
            if  !nmpo_state {
                NMPOCommon.nmpo_alertShow(nmpo_title: "Location", nmpo_message: "Request your location permission to obtain the speed information of your current network")
            }
        }
    }
}
